package com.ruoyi.netty.handler;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.netty.Command;
import com.ruoyi.netty.CommandType;
import com.ruoyi.netty.Result;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.Objects;

/**
 * @author Zoo
 */
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) throws Exception {
        try{
            System.out.println("报文消息-"+frame.text());
            Command command = JSON.parseObject(frame.text(), Command.class);

            switch (CommandType.match(command.getCode())) {
                    //连接01
                case CONNECTION:
                    ConnectionHandler.execute(ctx, command);
                    break;
                    //发信息02
                case CHAT:
                    ChatHandler.execute(ctx, frame);
                    break;
                    //加入 群聊
                case JOIN_GROUP:
                    JoinGroupHandler.execute(ctx);
                    break;
                    //接收坐标
                case COORDINATE:
                    CoordinateHandler.execute(ctx, frame);
                    break;
                    //接收订单
                case ORDER_FORM:
                    OrderFormHandler.execute(ctx, frame);
                    break;
                default:
                    ctx.channel().writeAndFlush(Result.fail("不支持的CODE"));
                    break;
            }

        }catch (Exception e){
            ctx.channel().writeAndFlush(Result.fail(e.getMessage()));
        }
    }
}
